package JavaDowork.pratice6;

import java.util.Scanner;

public class Q5 {

    public static void main(String[] args) {
        // 定义并初始化一个数组
        int[] numbers = {5, 3, 9, 6, 8, 2, 1, 7, 4, 0};

        // 从控制台输入要查找的数字
        System.out.print("请输入要查找的数字：");
        int target = new Scanner(System.in).nextInt();

        // 查找数字并输出结果
        int index = search(numbers, target);
        if (index != -1) {
            System.out.println(target + "是数组中的第" + (index + 1) + "个数");
        } else {
            System.out.println("查无此数");
        }
    }

    // 查找数字并返回其索引的方法
    static int search(int[] numbers, int target) {
        for (int i = 0; i < numbers.length; i++) {
            if (numbers[i] == target) {
                return i;
            }
        }
        return -1;
    }
}
